package com._9xcloud.leaverequest.service;

import com._9xcloud.leaverequest.domain.College;
import com._9xcloud.leaverequest.domain.Teacher;
import com._9xcloud.leaverequest.domain.TeacherLevel;
import com._9xcloud.leaverequest.exception.InvalidPasswordException;
import com._9xcloud.leaverequest.exception.NotFoundException;
import com._9xcloud.leaverequest.repository.TeacherRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created by hyq on 2015/5/10.
 */

@Repository
@Transactional
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherRepository teacherRepository;

    @Transactional(readOnly = true)
    public Teacher authenticate(String username, String password) throws AuthenticationException {
        Teacher teacher = loadUserByUsername(username);
        if (!teacher.getPassword().contentEquals(password)) {
            throw new BadCredentialsException("Invalid password");
        }
        return teacher;
    }

    @Transactional(readOnly = true)
    public Teacher loadUserByUsername(String username) throws UsernameNotFoundException {
        Teacher teacher = teacherRepository.findByUsername(username);
        if (teacher == null) {
            throw new UsernameNotFoundException("Could not find the user");
        }
        return teacher;
    }

    @Transactional(readOnly = true)
    public Teacher getById(int teacherId) {
        Teacher teacher = teacherRepository.findOne(teacherId);
        if (teacher == null) {
            throw new NotFoundException("Could not find the user");
        }
        return teacher;
    }

    public void changePassword(int teacherId, String oldPassword, String newPassword) {
        Teacher teacher = getById(teacherId);
        if (!teacher.getPassword().contentEquals(oldPassword)) {
            throw new InvalidPasswordException("The old password is incorrect");
        }
        if (StringUtils.isEmpty(newPassword)) {
            throw new InvalidPasswordException("The new password can not be empty");
        }
        teacher.setPassword(newPassword);
        teacherRepository.saveAndFlush(teacher);
    }

    public Teacher create(String fullName, String username, String password, College college, TeacherLevel level) {
        Teacher teacher = new Teacher(fullName, username, password, college, level);
        return teacherRepository.saveAndFlush(teacher);
    }

    public void removeAll() {
        teacherRepository.deleteAll();
        teacherRepository.flush();
    }

    @Transactional(readOnly = true)
    public List<Teacher> getAll() {
        return teacherRepository.findAll();
    }

    @Transactional(readOnly = true)
    public List<Teacher> getAllByCollegeId(int collegeId) {
        return teacherRepository.findByCollegeId(collegeId);
    }
}
